package com.topnav.mc.admin.dao.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topnav.mc.admin.controller.FullScreenMoniter.ChartDataBean;
import com.topnav.mc.admin.entity.bean.DeviceBean;
import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.base.mc.McMapper;
import org.apache.ibatis.annotations.Mapper;
import com.topnav.mc.admin.entity.Device;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;

@Mapper
//public interface DeviceMapper extends BaseMapper<Device> {
public interface DeviceMapper extends McMapper<Device, DeviceBean, DeviceParam> {
    String commSql = " SELECT d.*, " +
            " o.code AS orgCode, o.name AS orgName, o.short_name AS shortName, o.path," +
            " u.code AS userCode, u.name AS userName," +
            " g.code AS gbServerCode, g.name AS gbServerName, g.host AS gbServerHost, g.port AS gbServerPort, " +
            " g.domain AS gbServerDomain, g.user_name AS gbServerUserName, g.passwrd AS gbServerPasswrd, " +
            " g.media_port AS gbServerMediaPort, g.api_port AS gbServerApiPort," +
            " g.city_id AS cityId,g.business_id AS businessId, g.net_identity_id AS netIdentityId, " +
            " g.center_type_id AS center_type_id, g.device_type_id AS deviceTypeId " +
            " FROM mc_device d " +
            " LEFT JOIN mc_organization o ON d.org_id = o.id " +
            " LEFT JOIN mc_user u ON d.user_id = u.id " +
            " LEFT JOIN mc_gb_server g ON d.gb_server_id = g.id " +
            " ${ew.customSqlSegment}";


    @Select(commSql)
    DeviceBean selectInfoOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    List<DeviceBean> selectInfoList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    Page<DeviceBean> selectInfoPage(Page<DeviceParam> page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

    /**
     * 根据输入的值搜索
     * @param param
     * @return
     */
    /*List<OrgDeviceVo> searchByValue(Map<String,Object> param);*/

    /**
     * 从device 表里取得在线状态
     * @param codes
     * @return
     */
/*    @Select("SELECT deviceId AS CODE,online AS note FROM mc_device WHERE deviceId in ${codes}")
    List<Device> getOnlineStatusFromOtherDevice(@Param("codes") String codes);*/

    /**
     *统计各类型的注册设备数
     */
    @Select("SELECT d.cate as type,count(*) as total FROM mc_device d GROUP BY d.cate")
    List<Map<String,Object>> countDevices();


    /**
     * 统计通道数量
     * return
     *   orgId,deviceId,channelCount,orgCode,path
     */

    String countByOrgSql = " SELECT d.org_id AS orgId,d.id AS deviceId,sum(d.channel_count) AS channelCount," +
            " o.code AS orgCode, o.path " +
            " FROM mc_device d " +
            " LEFT JOIN mc_organization o ON d.org_id = o.id " +
            " ${ew.customSqlSegment} " ;
    //" GROUP BY d.org_id,c.device_id ";
    @Select(countByOrgSql)
    List<Map<String,Object>> sumChannel(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    /**
     * 按类型统计
     * @param
     * @return
     * orgId,id
     */
    @Select(
            "SELECT d.type AS intId,COUNT(1) AS value \n" +
                    "FROM mc_device d \n" +
                    "LEFT JOIN mc_organization o ON d.org_id = o.id\n" +
                    "LEFT JOIN mc_user u ON d.user_id = u.id\n" +
                    "${ew.customSqlSegment}" +
                    "GROUP BY d.type")

    List<ChartDataBean> countByType(@Param(Constants.WRAPPER) Wrapper queryWrapper);


    /**
     * 统计设备注册总数
     * @param queryWrapper
     * @return
     */
    @Select(
            "SELECT COUNT(1) AS value \n" +
                    "FROM mc_device d \n" +
                    "LEFT JOIN mc_organization o ON d.org_id = o.id\n" +
                    "LEFT JOIN mc_user u ON d.user_id = u.id\n" +
                    "${ew.customSqlSegment}")
    List<ChartDataBean> countAll(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    /**
     * 按部门统计
     * @param queryWrapper
     * @return
     */
    @Select(
            "SELECT t.*,o.short_name AS name \n" +
                "FROM (\n" +
                    "SELECT d.org_id AS longId,COUNT(1) AS value \n" +
                    "FROM mc_device d \n" +
                    "LEFT JOIN mc_organization o ON d.org_id = o.id\n" +
                    "LEFT JOIN mc_user u ON d.user_id = u.id\n" +
                    "${ew.customSqlSegment} \n" +
                    "GROUP BY d.org_id\n" +
                    ") t\n" +
                "LEFT JOIN mc_organization o ON t.longId = o.id\n" +
                "ORDER BY t.`value` DESC")
    List<ChartDataBean> countByOrg(@Param(Constants.WRAPPER) Wrapper queryWrapper);


}

